package com.keon.jianbao.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseFragment;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.ui.activity.TweetPublishActivity;
import com.keon.jianbao.util.JsonUtils;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by keon on 2015/10/16.
 */
public class MainTweetListFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    FragmentManager fragmentManager;

    ArrayList<String> catlogs;
    String selectedCatlog;

    boolean show = false;
    int type = 2;

    @Bind(R.id.tweet_list_toolbar)
    Toolbar tbToolBar;
    @Bind(R.id.tweet_list_catlog)
    TextView tvCatlog;
    @Bind(R.id.tweet_list_publish)
    ImageView ivPublish;
    @Bind(R.id.tweet_list_toolbarTab)
    LinearLayout llTab;
    @Bind(R.id.tweet_list_toolbar_title_tab1)
    protected TextView tvTab1;
    @Bind(R.id.tweet_list_toolbar_title_tab2)
    protected TextView tvTab2;
    @Bind(R.id.list)
    FrameLayout flList;
    @Bind(R.id.tweet_list_catlog_list)
    ListView lvCatlog;

    SimpleHandler catlogHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    catlogs = JsonUtils.fromJson(result.getContent(), ArrayList.class);
                    catlogs.add(0, "全部");
                    initCatlogList(catlogs);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tweet_list;
    }

    @Override
    public void initView(View view) {
        fragmentManager = getActivity().getSupportFragmentManager();
        tvCatlog.setOnClickListener(this);
        ivPublish.setOnClickListener(this);
        lvCatlog.setOnItemClickListener(this);

        setToolBarTab(R.string.tweet_list_hot, R.string.tweet_list_new);

    }

    @Override
    public void initData() {

        catlogHandler.setContext(getActivity());
        showWaitDialog();
        Api.appraisal_catalog(catlogHandler);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tweet_list_publish:
                BaseActivity ba = (BaseActivity) getActivity();
                ba.jump(TweetPublishActivity.class);
            case R.id.tweet_list_toolbar_title_tab1:
            case R.id.tweet_list_toolbar_title_tab2:
                chooseToolbarTab(id);
                loadData();
                break;
            case R.id.tweet_list_catlog:
                if (!show) {
                    lvCatlog.setVisibility(View.VISIBLE);
                    flList.setVisibility(View.GONE);
                    show = true;
                } else {
                    flList.setVisibility(View.VISIBLE);
                    lvCatlog.setVisibility(View.GONE);
                    show = false;
                }

                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    void loadData() {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        TweetListFragment tweetListFragment = new TweetListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("page", 1);
        bundle.putString("selectedCatlog", selectedCatlog);
        tweetListFragment.setArguments(bundle);
        ft.replace(R.id.list, tweetListFragment).commit();
    }

    void initCatlogList(ArrayList catlogs) {
        lvCatlog.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_cell_catlog, catlogs));
    }

    protected void setToolBarTab(int tab1, int tab2) {
        llTab.setVisibility(View.VISIBLE);
        tvTab1.setText(tab1);
        tvTab1.setOnClickListener(this);
        tvTab2.setText(tab2);
        tvTab2.setOnClickListener(this);
    }

    protected void removeToolBarTab() {
        llTab.setVisibility(View.GONE);
    }

    public void chooseToolbarTab(int id) {
        if (id == R.id.tweet_list_toolbar_title_tab1) {
            type = 2;
            tvTab1.setTextAppearance(getActivity(), R.style.toolbarTabPressed);
            tvTab1.setBackgroundResource(R.drawable.toolbar_title_tab_pressed_bg);
            tvTab2.setTextAppearance(getActivity(), R.style.toolbarTab);
            tvTab2.setBackgroundResource(R.drawable.toolbar_title_bg);
        } else if (id == R.id.tweet_list_toolbar_title_tab2) {
            type = 1;
            tvTab2.setTextAppearance(getActivity(), R.style.toolbarTabPressed);
            tvTab2.setBackgroundResource(R.drawable.toolbar_title_tab_pressed_bg);
            tvTab1.setTextAppearance(getActivity(), R.style.toolbarTab);
            tvTab1.setBackgroundResource(R.drawable.toolbar_title_bg);
        }
        selectedCatlog = "";
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        lvCatlog.setVisibility(View.GONE);
        flList.setVisibility(View.VISIBLE);
        show = false;
        if (i != 0) {
            selectedCatlog = catlogs.get(i);
        } else {
            selectedCatlog = "";
        }

        loadData();
    }
}
